// pages/good_detail/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodData:{},
    isCollect:false,
    collect:[]

  },
  goodsFullData:{},
  onShow: function () {
    // console.log(options);
    let pages = getCurrentPages();
    // console.log(pages);
    let currentPage = pages[pages.length - 1];
    // console.log(currentPage);
    // console.log(currentPage);
    let options = currentPage.options;
    // console.log(options);
    const {goods_id} = options;
    // console.log(goods_id);
    this.getGoodData(goods_id);
  },

  async getGoodData(goods_id){
    const goodData = await request({url:"/goods/detail", data:{goods_id}});
    // console.log(goodData);
    this.goodsFullData = goodData;
    // 1.获取缓存中的商品收藏数组
    let collect=wx.getStorageSync("collect")||[];
    // 2.判断该商品是否被收藏
    let isCollect = collect.some(v=>{return v.goods_id===this.goodsFullData.goods_id});
    // console.log(this.goodsFullData);
    this.setData(
      {
      goodData:{
        goods_name: goodData.goods_name,
        goods_price: goodData.goods_price,
        // 80q.webp iphone部分手机不识别webp格式的图片，我们用正则规则结合replace代替goods_introduce中.webp为.jpg
        goods_introduce: goodData.goods_introduce.replace(/\.webp/g,'.jpg'),
        pics: goodData.pics,
        goods_id: goodData.goods_id
      },
      isCollect
    })
  },

  handleAdd:function(){
    /* 
        1.获取本地缓存中的购物车数组，加入或[]防止获取不到数组，即获取不到时重置一个空数组
        2.获取当前点击的商品索引
        3.判断缓存数组中是否存在有该商品
            若有，则数量+1
            若没有，则加入该商品
        4.将新数组写入缓存
        5.提示添加成功
     */
    //  console.log(e);
    let userInfo = wx.getStorageSync('userInfo') || {};
    if(Object.keys(userInfo).length === 0){
      wx.showToast({
        title: '请您先登陆',
        mask: true,
        icon: "none"
      });
      return;
    }
    let cart = wx.getStorageSync("cart") || [];
    let index = cart.findIndex(v=>v.goods_id===this.goodsFullData.goods_id)
    if(index === -1){
      // 没有库存
      this.goodsFullData.num = 1;
      this.goodsFullData.selected = false;
      cart.push(this.goodsFullData);
    }else{
      // 有库存
      cart[index].num ++;
    }
    // 拿什么出来，设置后放什么回去
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '加入成功',
      duration: 1000,
      mask: true
    });
  },

  // 看大图功能
  handlePreviewImage: function(e){
    const urls = this.goodsFullData.pics.map(v=>v.pics_mid);
    const currentUrl = e.currentTarget.dataset.url;
    wx.previewImage({
      urls:urls,
      current:currentUrl,
      showmenu:true
    })
  },

  handleCollect(e){
    // console.log(e);
    let isCollect = false;
    let collect = wx.getStorageSync('collect') || [];
    let userInfo = wx.getStorageSync('userInfo') || {};
    if(Object.keys(userInfo).length === 0){
      wx.showToast({
        title: '请您先登陆',
        mask: true,
        icon: "none"
      });
      return;
    }
    // console.log(collect);
    let index = collect.findIndex(v=>v.goods_id === this.goodsFullData.goods_id);
    // console.log(index);
    if(index !== -1){
      // 收藏过，从缓存中删除，更新数据
      collect.splice(index, 1);
      console.log(collect);
      isCollect = false;
      wx.showToast({
        title: '取消成功',
        icon: 'success',
        mask: true
      })
    }else{
      //没收藏过，加入缓存，更新数据
      // !!!!!!!!!!!!这里是 this.data.goodData，而不是this.goodData
      collect.push(this.data.goodData);
      isCollect = true;
      console.log(collect);
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      })
    }
    wx.setStorageSync('collect', collect);
    this.setData({
      isCollect,
      collect
    })
  },

  handlePay(){
    this.goToUrl("/pages/directBuy/index");
  },


  goToUrl(url){
    const userInfo = wx.getStorageSync('userInfo') || {};
    if(Object.keys(userInfo).length === 0){
      wx.showToast({
        title: '请您先登陆',
        mask: true,
        icon: "none"
      });
    }else{
      wx.navigateTo({
        url: url
      })
    }
  },

  goCart(){
    const userInfo = wx.getStorageSync('userInfo') || {};
    if(Object.keys(userInfo).length === 0){
      wx.showToast({
        title: '请您先登陆',
        mask: true,
        icon: "none"
      });
    }else{
      wx.switchTab({
        url:"/pages/cart/index"
      })
    }
  }
})